package com.activity.shop;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import event.MessageEvent;

import android.os.Bundle;

import com.activity.adapter.ClassSetAdapter;
import com.activity.bean.ClassSpan;
import com.api.param.ClassParam;
import com.api.param.StoreParam;
import com.api.viewModel.StoreVM;
import com.example.apple.ddhr_new.R;
import com.example.apple.ddhr_new.activity.setting.BaseListViewActivity;
import com.google.gson.Gson;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;

public class ClassActivity extends BaseListViewActivity<StoreVM> {

    private StoreParam  param= new StoreParam();
    private ClassSetAdapter adapter;
    public ArrayList<ClassSpan> totals= new ArrayList<>();
    private String currenttext="";

    @Override
    protected int titleText() {
        return R.string.banci_shezhi ;
    }

    @Override
    protected void created(@Nullable Bundle savedInstanceState) {
        super.created(savedInstanceState);
        refreshLayout.setEnableLoadMore(false);
        adapter = new ClassSetAdapter(this);
        recycleView.setAdapter(adapter);

        listViewModel.classSpan.observe(this,res->{
            if(res!=null){

                for (int i=0;i<totals.size();i++){

                    for (int j = 0;j<res.size();j++){
                        if(totals.get(i).getAttendacetimespanid().equals(res.get(j).getAttendacetimespanid())){
                            res.get(j).selected =totals.get(i).selected;

                            break;
                        }
                    }

                }

                totals = res;
                textChanged(currenttext);

            }
            headerFreshEnd();
        });
        headerFreshing();
        adapter.classListener = new ClassSetAdapter.ClassListener() {
            @Override
            public void textChange(String text) {
                currenttext = text;
                textChanged(text);
            }
        };
    }
    private  void  textChanged(String text) {
        if(StringUtils.isEmpty(text)){

            adapter.getData().clear();
            adapter.getData().addAll(totals);

        }else
        {
             ArrayList<ClassSpan> classSpan =  new ArrayList<>();
            for(int i=0;i<totals.size();i++){
                if(totals.get(i).getAttendacetimespanname().contains(text)){
                    classSpan.add(totals.get(i));
                }
            }
            adapter.getData().clear();
            adapter.getData().addAll(classSpan);


        }
        if (recycleView.getScrollState() == RecyclerView.SCROLL_STATE_IDLE
                && !recycleView.isComputingLayout()) {
            adapter.notifyGroupChanged(0);
        }
        showEmpty(adapter.getData().size()==0);

    }
    @Override
    protected void headerFreshing() {
        addSubscription(listViewModel.getClass(param));
    }

    @Override
    public void onMessageEvent(MessageEvent event) {
        super.onMessageEvent(event);
        if(event.getMessage() == MessageEvent.MESSAGE_EVENT_ADD_CLASS){



            headerFreshing();

        }else  if(event.getMessage() == MessageEvent.MESSAGE_EVENT_DELETE_CLASS){

            for (int i=0;i<adapter.getData().size();i++){
                if(adapter.getData().get(i).getAttendacetimespanid().equals(event.spamId)){


                    adapter.getData().remove(i);
                    break;
                }
            }

            for (int i=0;i<totals.size();i++){
                if(totals.get(i).getAttendacetimespanid().equals(event.spamId)){


                    totals.remove(i);
                    break;
                }
            }

            adapter.notifyDataChanged();
        }
        else  if(event.getMessage() == MessageEvent.MESSAGE_EVENT_UPDATE_CLASS){

            Gson gson = new Gson();
            ClassParam param = gson.fromJson(event.spanParam,ClassParam.class);

            for (int i=0;i<adapter.getData().size();i++){
                if(adapter.getData().get(i).getAttendacetimespanid().equals(param.getTimespanid())){

                    adapter.getData().get(i).setAttendacetimespan_begintime(param.getBegintime());

                    adapter.getData().get(i).setAttendacetimespan_endtime(param.getEndtime());
                    adapter.getData().get(i).setAttendacetimespanname(param.getTimespanname());
                    adapter.getData().get(i).setAttendacetimespan_timelength(param.getTimelength());
                    break;
                }
            }

            for (int i=0;i<totals.size();i++){
                if(totals.get(i).getAttendacetimespanid().equals(param.getTimespanid())){


                    totals.get(i).setAttendacetimespan_begintime(param.getBegintime());

                    totals.get(i).setAttendacetimespan_endtime(param.getEndtime());
                    totals.get(i).setAttendacetimespanname(param.getTimespanname());
                    totals.get(i).setAttendacetimespan_timelength(param.getTimelength());
                    break;
                }
            }

            adapter.notifyDataChanged();
        }
    }


}
